package com.gallop.mq;

import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@AutoConfigureAfter(MQAutoConfiguration.class)
public class MockMQTemplateBean {

    @Bean
    @ConditionalOnMissingBean(MQTemplate.class)
    @ConditionalOnProperty(prefix = "gallop.mq", value = "mock", havingValue = "true")
    public MQTemplate defaultMQTemplate() throws InterruptedException {
        MQTemplate mock = Mockito.mock(MQTemplate.class);
        Mockito.when(mock.request(MQTemplateTest.Payload.builder()
                        .id("test123")
                        .data("message transfer")
                        .build(),
                MQTemplateTest.TEST_PAYLOAD_TOPIC)).thenReturn(MQTemplateTest.PayloadResponse.builder()
                .data("response" + "message transfer")
                .id("response" + "test123")
                .build());
        return mock;
    }
}
